{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 茅台天猫爬虫https://maotai.tmall.com\n",
    "# 包含商品名、价格、销量\n",
    "import requests\n",
    "import time\n",
    "from bs4 import BeautifulSoup\n",
    "import csv, codecs\n",
    "from tqdm import tqdm\n",
    "import re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "tmall_url = 'https://maotai.tmall.com/i/asynSearch.htm?_ksTS=1502379704428_364&callback=jsonp365&mid=w-15656860787-0&wid=15656860787&path=/search.htm&search=y&spm=a1z10.1-b-s.w5003-15656860766.41.4c03279NWdbir&scene=taobao_shop'\n",
    "data_date = time.strftime('%Y-%m-%d',time.localtime(time.time()))\n",
    "\n",
    "headers = {\n",
    "    'User-Agent': 'Mozilla/5.0'\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def get_data(url):\n",
    "    r = requests.get(url, headers=headers)\n",
    "    soup = BeautifulSoup(r.text, 'lxml')\n",
    "    contents = soup.find_all('dd')\n",
    "    data = set()\n",
    "    for i in tqdm(contents):\n",
    "        try:\n",
    "            title = i.find('a').string.strip()\n",
    "            prices = i.find('div')\n",
    "            price_ = prices.find_all('span')\n",
    "            price = price_[1].string.strip()\n",
    "            sell_count = price_[2].string.strip()\n",
    "            product_info = (data_date, title, price, sell_count)\n",
    "            data.add(product_info)\n",
    "        except:\n",
    "            pass\n",
    "    return data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def save_data(data):\n",
    "    with codecs.open(r'maotai_tmall.csv', 'a', encoding='utf_8_sig') as f:\n",
    "        csv_file = csv.writer(f, dialect='excel')\n",
    "        for i in tqdm(data):\n",
    "            csv_file.writerow(i)\n",
    "    return "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████████████████████████████████| 102/102 [00:00<00:00, 2831.29it/s]\n",
      "100%|███████████████████████████████████████| 46/46 [00:00<00:00, 23007.15it/s]\n"
     ]
    }
   ],
   "source": [
    "if __name__ == '__main__':\n",
    "    data = get_data(tmall_url)\n",
    "    save_data(data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "hide_input": false,
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  },
  "toc": {
   "colors": {
    "hover_highlight": "#DAA520",
    "navigate_num": "#000000",
    "navigate_text": "#333333",
    "running_highlight": "#FF0000",
    "selected_highlight": "#FFD700",
    "sidebar_border": "#EEEEEE",
    "wrapper_background": "#FFFFFF"
   },
   "moveMenuLeft": true,
   "nav_menu": {
    "height": "12px",
    "width": "252px"
   },
   "navigate_menu": true,
   "number_sections": true,
   "sideBar": true,
   "threshold": 4,
   "toc_cell": false,
   "toc_section_display": "block",
   "toc_window_display": false,
   "widenNotebook": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
